package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.BillDetailDao;
import com.juyu.miliao.api.dao.redis.BeanWelfareRedisDao;
import com.juyu.miliao.api.domain.BeanWelfare;
import com.juyu.miliao.common.constant.BeansType;
import com.juyu.miliao.common.domain.BillDetailBeans;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/**
 * @ClassName: BeanWelfareService
 * @Description: TODO
 * @Author zhangcheng
 * @Date 19-9-18上午9:06
 */
@Service
public class BeanWelfareService {
    private final List<BeanWelfare> beanWelfareList;
    @Resource
    private BeanWelfareRedisDao beanWelfareRedisDao;
    @Resource
    private UserWalletService userWalletService;
    @Resource
    private BillDetailDao billDetailDao;

    public BeanWelfareService() {
        beanWelfareList = new ArrayList<>(4);
        beanWelfareList.add(new BeanWelfare(LocalTime.of(9, 0), LocalTime.of(10, 0), 1000L));
        beanWelfareList.add(new BeanWelfare(LocalTime.of(12, 0), LocalTime.of(13, 0), 1000L));
        beanWelfareList.add(new BeanWelfare(LocalTime.of(18, 0), LocalTime.of(19, 0), 1000L));
        beanWelfareList.add(new BeanWelfare(LocalTime.of(21, 0), LocalTime.of(22, 0), 1000L));
    }

    public List<Integer> getBeanWelfare(Long userId) {
        List<Integer> statusList = beanWelfareRedisDao.getAwardStatusList(userId);
        return IntStream.range(0, 4)
                .map(index -> {
                    int status = statusList.get(index);
                    if (status == 2) {
                        return 2;
                    } else {
                        LocalTime now = LocalTime.now();
                        BeanWelfare beanWelfare = beanWelfareList.get(index);
                        if (beanWelfare.getEndTime().isBefore(now)) {
                            return 3;
                        } else if (beanWelfare.getStartTime().isBefore(now) && beanWelfare.getEndTime().isAfter(now)) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                })
                .boxed()
                .collect(toList());
    }

    public void award(Long userId, Long rewardId) {
        BeanWelfare beanWelfare = beanWelfareList.get((int) (long) rewardId);
        LocalTime now = LocalTime.now();
        if (beanWelfare.getStartTime().isBefore(now) && beanWelfare.getEndTime().isAfter(now)) {
            boolean bool = beanWelfareRedisDao.award(userId, rewardId);
            if (bool) {
                userWalletService.addBeans(userId, beanWelfare.getBeans());
                BillDetailBeans billDetailBeans = new BillDetailBeans();
                billDetailBeans.setBeansType(BeansType.BEAN_WELFARE.getCode());
                billDetailBeans.setBeans(beanWelfare.getBeans());
                billDetailBeans.setRatio(0);
                billDetailBeans.setUserId(userId);
                billDetailDao.insertBillDetailBeans(billDetailBeans);
            } else {
                throw new IllegalParamException("已领取");
            }
        } else {
            throw new IllegalParamException("不符合领取条件");
        }
    }

}
